package easy;

import util.PrintUtils;

import java.util.*;

public class Solution_821 {

    public int[] shortestToChar(String s, char c) {
        int[] ints = new int[s.length()];
        char[] chars = s.toCharArray();
        int beginIndex = -1, endIndex = -1;
        for (int i = 0, count = 1; i < chars.length; i++) {
            if (chars[i]==c) {
                ints[i] = 0;
                count = 1;
                if (beginIndex == -1) {
                    beginIndex = i;
                }
                endIndex = i;
            } else {
                ints[i] = count;
                count++;
            }
        }
        for (int i = endIndex,count=1; i > beginIndex; i--) {
            if (ints[i]==0) {
                count=1;
            } else {
                if (ints[i]>count) {
                    ints[i] = count;
                    count++;
                }
            }
        }
        for (int i = beginIndex-1,count=1; i >= 0; i--) {
            ints[i] = count++;
        }
        return ints;
    }

    public static void main(String[] args) {
        Solution_821 model = new Solution_821();
        PrintUtils.arrayInt(model.shortestToChar("loveleetcode", 'e'));
    }
}
